﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyArrow : MonoBehaviour {
    public MeshRenderer render;
    public Transform EnemyPivot;
    public GameObject EnemytroopPrefab;
    public float InitTime;
    public float currentTime;
    public Color barColor;

    public DotLineRender dotLineRender;
    public BeamRayInteractive beamRayInteractive;
    public Transform target;


    

    private void Update()
    {
        currentTime += Time.deltaTime;
        render.material.SetFloat("_Prograss", currentTime / InitTime);
        render.material.SetColor("_BarColor", barColor);

        if(currentTime>InitTime)
        {
            Vector3 posOnNavMesh = NavMeshExtension.SampleNearestPositionOnWalkableNavMesh(EnemyPivot.position, 20f);
            Instantiate(EnemytroopPrefab, posOnNavMesh, EnemyPivot.rotation);
            EnemytroopPrefab.GetComponent<TroopVehicle>().targetPosition = target.position;
            Destroy(this.gameObject);
        }

        if(beamRayInteractive.hitRay!=null)
        {
            dotLineRender.isRender = true;
            dotLineRender.ResetPos(transform.position, target.position);
        }
        else
        {
            dotLineRender.isRender = false;
        }


        
    }

}
